//
//  ChangePhoneTextCell.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/3/7.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit

protocol ChangePhoneTextCellDelegate: NSObjectProtocol {
    func textCell(_ cell: ChangePhoneTextCell, didEndEdit text: String)
}

class ChangePhoneTextCell: UITableViewCell, UITextFieldDelegate {
    
    weak var delegate: ChangePhoneTextCellDelegate?
    
    @IBOutlet private weak var titleLabel: UILabel!
    
    @IBOutlet private weak var textField: UITextField! {
        didSet {
            textField.delegate = self
        }
    }
    
    @IBOutlet private weak var splitLine: UIView!
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        delegate?.textCell(self, didEndEdit: textField.text!)
    }
    
    func configure(at indexPath: IndexPath, titles: [String], vm: FamilyAddViewModel) {
        titleLabel.textColor = indexPath.section == 0 ? .textGray : .textDark
        titleLabel.text = titles[indexPath.row]
        textField.isEnabled = indexPath.section != 0
        textField.textColor = indexPath.section == 0 ? .textGray : .textDark
        switch indexPath.section {
        case 0: setInfo(at: indexPath, vm: vm)
        default: break
        }
    }
    
    private func setInfo(at indexPath: IndexPath, vm: FamilyAddViewModel) {
        switch indexPath.row {
        case 0: textField.text = vm.name
        case 1: textField.text = vm.idcard
        case 2: textField.text = vm.area
        case 3: textField.text = vm.addr
        case 4: textField.text = vm.reservedPhone
        default: break
        }
    }
}
